package org.abadirou.cdebacker.flst.entity;

import java.util.Calendar;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

import org.abadirou.cdebacker.flst.api.Chambre;
import org.abadirou.cdebacker.flst.api.Reservation;
import org.joda.time.DateTime;

@NamedQueries({
	@NamedQuery(name = "RESERVATION_BY_PERSONNE", query = "FROM Reservation WHERE personne = :personne"),
	@NamedQuery(name = "RESERVATION_BY_CHAMBRE", query = "FROM Reservation WHERE chambre = :chambre"),
	@NamedQuery(name =  "ALL_RESERVATION", query = "FROM Reservation ORDER BY 1"),
	@NamedQuery(name = "RESERVATION_BY_PERIOD", query = "FROM Reservation r WHERE r.dateEntre >= :from and r.dateSortie <= :to"),
	@NamedQuery(name = "RESERVATION_NOW", query = "FROM Reservation r WHERE r.dateEntre <= :now and r.dateSortie >= :now")
})
@Entity(name = "Reservation")
public class ReservationImpl implements Reservation {

	public static final String RESERVATION_BY_PERSONNE = "RESERVATION_BY_PERSONNE";
	public static final String RESERVATION_BY_CHAMBRE = "RESERVATION_BY_CHAMBRE";
	public static final String ALL_RESERVATION = "ALL_RESERVATION";
	public static final String RESERVATION_BY_PERIOD = "RESERVATION_BY_PERIOD";
	public static final String RESERVATION_NOW = "RESERVATION_NOW";

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE)
	private Long id;

	@ManyToOne
	@JoinColumn(name = "personne_id", nullable = false)
	private final PersonneImpl personne;

	@ManyToOne
	@JoinColumn(name = "chambre_id", nullable = false)
	private final ChambreImpl chambre;

	private final Calendar dateEntre;
	private final Calendar dateSortie;

	public ReservationImpl(PersonneImpl personne, ChambreImpl chambre,
			DateTime dateTime, DateTime dateTime2) {

		this.personne = personne;
		this.chambre = chambre;
		this.dateEntre = Calendar.getInstance();
		this.dateEntre.setTime(dateTime.toDate());
		this.dateSortie = Calendar.getInstance();
		this.dateSortie.setTime(dateTime2.toDate());
	}

	@Override
	public Chambre getChambre() {
		return chambre;
	}

	
}
